import { User } from "../../models/user.model";
import { GlobalService } from "../../services/global.service";
import { LocalStorage } from "../../services/local-storage.service";
import { UserService } from "../../services/user.service";

const defaultAvatarUrl = `/assets/avatar.png`;
// pages/user/user.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatarUrl: '',
      nickName: '',
    },
    visible: false,
    FILE_HOST: GlobalService.STATIC_FILE_HOST,
    GROUP: 'https://cardarena.site/marketing/test.png',
    imageOnMiddle: false,
    version: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getTabBar().setData({
      value: '/pages/user/user'
    });
    wx.setNavigationBarTitle({
      title: '我的'
    });
    const data = wx.getAccountInfoSync();
    console.log(data)
    this.setData({
      version: data.miniProgram.version
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const userInfo = LocalStorage.getUserInfo();
    if (!userInfo) {
      wx.login({
        success: async (response) => {
          const userInfo = await UserService.login(response.code);
          this.init(userInfo);
          LocalStorage.setUserInfo(userInfo);
        }
      });
    } else {
      this.init(userInfo);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  edit() {
    wx.navigateTo({
      url: '/pages/user/user-info-edit/user-info-edit.page',
    })
  },

  init(userInfo: User) {
    this.setData({
      userInfo: {
        avatarUrl: !!userInfo.avatarUrl ? `${GlobalService.STATIC_FILE_HOST}${userInfo.avatarUrl}` : defaultAvatarUrl,
        nickName: !!userInfo.nickName ? userInfo.nickName : '还未设置昵称'
      }
    });
  },

  coin() {
    const myTurn = Math.random() > 0.5;
    this.setData({
      visible: true,
      myTurn,
    })
  },

  onVisibleChange(e: any) {
    this.setData({
      visible: e.detail.visible
    })
  },

  userTradeList() {
    wx.navigateTo({
      url: '/pages/user/user-trade-list/user-trade-list.page',
    });
  },

  userDeckList() {
    wx.navigateTo({
      url: '/pages/user/user-deck-list/user-deck-list.page',
    });
  },

  userVideoList() {
    wx.navigateTo({
      url: '/pages/video/user-video-list/user-video-list.page',
    });
  },

  pCalc() {
    wx.navigateTo({
      url: '/pages/tools/p-calc/p-calc.page',
    });
  },

  timer() {
    wx.navigateTo({
      url: '/pages/tools/timer/timer.page',
    });
  },

  couter() {
    wx.navigateTo({
      url: '/pages/tools/couter/couter.page',
    });
  },

  swiss() {
    wx.navigateTo({
      url: '/pages/tools/swiss-calc/swiss-calc.page',
    });
  },

  txc() {
    wx.openEmbeddedMiniProgram({
      appId: "wx8abaf00ee8c3202e",
      extraData: {
        id: "636123",
        customData: {}
      }
    });
  },

  group() {
    GlobalService.getAppConf().then((data: {
      uaUserGroupQR: string
    }) => {
      this.setData({
        GROUP: data.uaUserGroupQR,
        imageOnMiddle: true
      })
    })
  },

  closeGroup() {
    this.setData({
      imageOnMiddle: false
    })
  },

  rule() {
    wx.navigateTo({
      url: '/pages/tools/rule/rule.page',
    });
  },

  conan() {
    wx.navigateTo({
      url: `/pages/conan/conan?display=true`,
    });
  },

  dcg() {
    wx.navigateToMiniProgram({
      appId: 'wx9d03c73835811d81',
      path: `pages/home/index`
    });
  },

  opcg() {
    wx.navigateToMiniProgram({
      appId: 'wxac804dd53219b73a',
      path: `pages/home/index`
    });
  },

  cr() {
    wx.navigateToMiniProgram({
      appId: 'wx50d578ffb728e29a',
      path: `pages/home/index`
    });
  }
})